﻿using System.ComponentModel;
using System.Globalization;
using System.Web.UI;
using System;
using System.Web.UI.WebControls;

[DefaultProperty("Text"), ToolboxData("<{0}:CalendarTimeEntry runat=server></{0}:CalendarTimeEntry>")]
partial class Control_TimeEntry : System.Web.UI.UserControl
{

    [Bindable(true), Category("Data"), DefaultValue("")]
    public System.DateTime Text
    {
        get
        {
            CultureInfo UKCultureInfo = new CultureInfo("en-GB");

            string lHour = ddlHours.SelectedValue; //txtHour.Text.Trim().Length<2? "0" + txtHour.Text.Trim(): txtHour.Text.Trim();// (string.Len(String.Trim(txtHour.Text)) < 2 ? "0" + String.Trim(txtHour.Text) : String.Trim(txtHour.Text));
            string lMinute = ddlMinutes.SelectedValue;//txtMinute.Text.Trim().Length < 2 ? "0" + txtMinute.Text.Trim() : txtMinute.Text.Trim(); //(Strings.Len(Strings.Trim(txtMinute.Text)) < 2 ? "0" + Strings.Trim(txtMinute.Text) : Strings.Trim(txtMinute.Text));
           

            try
            {
                System.DateTime lDate = System.DateTime.Parse(txtDate.Text, UKCultureInfo);
            }
            catch (Exception ex)
            {
                txtDate.Text = string.Empty;
            }

            if (txtDate.Text.Equals(string.Empty))
            {
                return Text = System.DateTime.Parse("01/01/1900 00:00:00", UKCultureInfo);
            }
            else
            {
               return  Text = System.DateTime.Parse(txtDate.Text + " " + lHour + ":" + lMinute + ":00", UKCultureInfo);
            }
        }

        set
        {
            CultureInfo UKCultureInfo = new CultureInfo("en-GB");

            if (value.Equals(System.DateTime.Parse("01/01/1900", UKCultureInfo)))
            {
                txtDate.Text = string.Empty;
                ddlMinutes.SelectedValue = "00";
                ddlHours.SelectedValue = "00";
            }
            else
            {
                txtDate.Text =   value.ToString( "dd/MM/yyyy");                 
                ddlHours.SelectedValue =  value.ToString( "HH");
                ddlMinutes.SelectedValue = value.ToString("mm");
            }
        }
    }

    [Bindable(true), Category("Appearance"), DefaultValue("")]
    public Unit Width
    {
        get { return Width = txtDate.Width; }

        set { txtDate.Width = value; }
    }

    [Bindable(true), Category("Appearance"), DefaultValue("")]
    public string CssClass
    {
        get { return CssClass = txtDate.CssClass; }

        set
        {
            txtDate.CssClass = value;
            
        }
    }

    [Bindable(true), Category("Appearance"), DefaultValue("TopLeft")]
    public string PopupPosition
    {
        get {return PopupPosition = cceDate.PopupPosition.ToString(); }

        set { 
                    string position = value;
                    switch (position)
	                {
                        case  "TopLeft" :
                              cceDate.PopupPosition = AjaxControlToolkit.CalendarPosition.TopLeft ;
                            break;
                        case "BottomLeft":
                            cceDate.PopupPosition = AjaxControlToolkit.CalendarPosition.BottomLeft;
                            break;
                        case "BottomRight":
                            cceDate.PopupPosition = AjaxControlToolkit.CalendarPosition.BottomRight;
                            break;
		                default :
                            cceDate.PopupPosition = AjaxControlToolkit.CalendarPosition.TopRight;
                            break ;
	                }
                }
    }

    public void FirstOfMonth()
    {
        CultureInfo UKCultureInfo = new CultureInfo("en-GB");

        if (!txtDate.Text.Equals(string.Empty))
        {
            System.DateTime BOMDate = default(System.DateTime);

            BOMDate = System.DateTime.Parse("01" + txtDate.Text.Substring(2, 8), UKCultureInfo);

            txtDate.Text =  BOMDate.ToString( "dd/MM/yyyy");
        }
    }

    public void EndOfMonth()
    {
        CultureInfo UKCultureInfo = new CultureInfo("en-GB");

        if (!txtDate.Text.Equals(string.Empty))
        {
            System.DateTime EOMDate = default(System.DateTime);

            EOMDate = System.DateTime.Parse("01" + txtDate.Text.Substring(2, 8), UKCultureInfo);
            EOMDate = EOMDate.AddMonths(1);
            EOMDate = EOMDate.AddDays(-1);
            txtDate.Text =  EOMDate.ToString( "dd/MM/yyyy");
        }
    }

    public void StartOfDay()
    {
        ddlHours.SelectedValue = "00";
        ddlMinutes.SelectedValue = "00";
    }

    public void EndOfDay()
    {
        ddlHours.SelectedValue = "23";
        ddlMinutes.SelectedValue = "59";
    }

    [Bindable(true), Category("Appearance"), DefaultValue("")]
    public string Hour
    {
        get { return Hour = ddlHours.SelectedValue; }

        set
        {
            ddlHours.SelectedValue = value;
        }
    }

    [Bindable(true), Category("Appearance"), DefaultValue("")]
    public string Minute
    {
        get { return Minute = ddlMinutes.SelectedValue; }

        set
        {
            ddlMinutes.SelectedValue = value;
        }
    }
    

}